package com.zcsy.manage.owner.controller.mall;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.constants.OwnerConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.commons.util.DateUtils;
import com.zcsy.manage.model.base.CommonToken;
import com.zcsy.manage.model.base.ShopInfo;
import com.zcsy.manage.model.mall.GoodsComment;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.owner.service.common.IOCommonTokenService;
import com.zcsy.manage.service.mall.IGoodsCommentService;

/**
 * @Project: ownerConn
 * @Title: GoodsCommentController
 * @Package com.zcsy.manage.owner.controller.mall
 * @Description: 商品评论控制层
 * 
 * @author hujie hujie@duoooo.com
 * @date 2016年7月13日 下午3:44:26
 * @Copyright：2014 武汉恒数利通软件有限公司 All rights reserved.
 * @version 1.0
 */
@Controller
@RequestMapping(value = "goodsComment")
public class GoodsCommentController {
	
	@Autowired
	private IGoodsCommentService goodsCommentService;

	@Autowired
	private IOCommonTokenService commonTokenService;
	
	/**
	 * @Method: submitGoodsComment
	 * @Description 提交商品评论
	 * @param goodsId 商品id
	 * @param content 内容
	 * @param score 分数（1-5分）
	 * @param token
	 * @return 
	 * @author liuziyu liuziyuwri@foxmail.com
	 * @date 2016年7月13日
	 * @see 
	 */
	@RequestMapping(value = "submitGoodsComment")
    @ResponseBody
    public RequestJson submitGoodsComment(String goodsId, String content, String score, String token) {
        
        RequestJson result = new RequestJson();
        
        CommonToken commonToken = commonTokenService.selectByToken(token);
		// 根据token查询登录名
		String loginname = commonToken.getAccount();
		
		GoodsComment goodsComment = new GoodsComment();
		goodsComment.setId(UniqueNumberUtils.get18UniqueNumber());
		goodsComment.setGoodsId(goodsId);
		goodsComment.setContent(content);
		goodsComment.setUserAccount(loginname);
		goodsComment.setSendTime(DateUtils.gettimestamp());
		
		Integer scoreInteger = 0;
		try {
			scoreInteger = Integer.parseInt(score);
			if (scoreInteger >= 1 && scoreInteger <= 5) {
				goodsComment.setScore(scoreInteger);
				// 添加进数据库
				goodsCommentService.insert(goodsComment);
				result.setSuccess(true);
				result.setResultCode(OwnerConstants.RESULT_CODE_SUCCESS);
				result.setMsg("提交评论成功！");
				result.setObj(goodsComment);
			} else {
				result.setSuccess(false);
				result.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
				result.setMsg("评分应在1-5之间！");
			}
		} catch (Exception e) {
			result.setSuccess(false);
			result.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
			result.setMsg("评分应为数字！");
		}
		
        return result;
    }

	/**
	 * @Method: goodsCommentList
	 * @Description 商品评论列表接口
	 * @param goodsId 商品id
	 * @param token
	 * @return 
	 * @author liuziyu liuziyuwri@foxmail.com
	 * @date 2016年7月14日
	 * @see 
	 */
	@RequestMapping(value = "goodsCommentList")
    @ResponseBody
    public RequestJson goodsCommentList(String goodsId, String token) {
        
        RequestJson result = new RequestJson();
        // 根据商品id查询评论
        List<GoodsComment> goodsComment = goodsCommentService.selectCommentByGoodsId(goodsId);
        
        if (goodsComment != null && goodsComment.size() > 0) {
        	result.setSuccess(true);
			result.setResultCode(OwnerConstants.RESULT_CODE_SUCCESS);
			result.setMsg("查询评论成功！");
			result.setObj(goodsComment);
		} else {
			result.setSuccess(false);
			result.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
			result.setMsg("未查询到商品评论！");

		}
        return result;
        }
	
}
